/**
 * Created by Joe on 2017/7/5 0005.
 */
var Author = {
    fields: {
        selects: ["isActivate", "delFlag", "role","job"], //下拉框
        radios: [],//单选框
        checkboxs: [],//复选款
        textareas: [],//多行文本框
        selfDefine: [{"field": "metroLine ", "selector": "a.metro_line"}]
    }
};
$(document).ready(function () {
    initDatatables();
    if ($("#menu-list") && $("#menu-list").length > 0) {
        loadingTree();
    }
    initEvent();
});

function initEvent() {
    $('.btn-detail-user').on('click', function () {
        Utils.clearForm("#detail-form");
        var url = /*[[@{/findByUserId}]]*/ 'findByUserId';
        Utils.ajaxSubmit(url + "/" + $(this).siblings("input").val(), {}, function (data) {
            if (data.result == "success") {
                Utils.fillForms(data.content, Author.fields);
                $("#userdetail-modal").modal("toggle");
            }
            if (data.result == "fail") {
                Utils.errorAlert(data.errorMessage);
            }
        })

    });
    $('#btn-create-user').on('click', function () {
        Utils.clearForm("#create-user-form");
        $("#newUser-modal").modal("toggle");
    });
    $('#saveNewUser').on('click', function () {
        var url = /*[[@{/addAdminUser}]]*/ 'addAdminUser';
        var param = $("#create-user-form").serialize();
        Utils.ajaxSubmit(url, param, function (data) {
            if (data.result == "success") {
                location.reload();
            }
        })
    });
    $('#saveUser').on('click', function () {
        var url = /*[[@{/updateUser}]]*/ 'updateUser';
        var param = $("#user-detail-form").serialize();
        Utils.ajaxSubmit(url, param, function (data) {
            if (data.result == "success") {
                location.reload();
            }
        })
    });
    $('#cancelCreateUser').on('click', function () {
        $("#newUser-modal").modal("toggle");
    });
    $('#cancelUpdateUser').on('click', function () {
        $("#userdetail-modal").modal("toggle");
    });
    /** the module of author */
    $('.btn-detail-role').on('click', function () {
        $("#detail-menu-list").empty();
        var url = /*[[@{/findAuthorById}]]*/ 'findAuthorById';
        Utils.ajaxSubmit(url + "/" + $(this).siblings("input").val(), {}, function (data) {
            if (data.result == "success") {
                Utils.fillForms(data.content, Author.fields);
                initLayUITree("#detail-menu-list", data.content.children);
                $("#detail-modal").modal("toggle");
            }
            if (data.result == "fail") {
                Utils.errorAlert(data.errorMessage);
            }
        })
    });
    $('#btn-create-role').on('click', function () {
        $("#create-modal").modal("toggle");
    });
    $('#saveNewAuthor').on('click', function () {
        var url = /*[[@{/assignNewRoleAuthor}]]*/ 'assignNewRoleAuthor';
        var param = $("#create-form").serialize();
        Utils.ajaxSubmit(url, param, function (data) {
            if (data.result == "success") {
                location.reload();
            }
        })
    });
    $('#saveAuthor').on('click', function () {
        var url = /*[[@{/assignRoleAuthor}]]*/ 'assignRoleAuthor';
        var param = $("#detail-form").serialize();
        Utils.ajaxSubmit(url, param, function (data) {
            if (data.result == "success") {
                location.reload();
            }
        })
    });
    $('#btn-new-cancel').on('click', function () {
        $("#create-modal").modal("toggle");
    });
    $('#btn-update-cancel').on('click', function () {
        $("#detail-modal").modal("toggle");
    });
}

function loadingTree() {
    var url = /*[[@{/getMenuList}]]*/ 'getMenuList';
    Utils.ajaxSubmit(url, {}, function (data) {
        if (data.result == "success") {
            initLayUITree("#menu-list", data.content)
        }
    });
}
function initLayUITree(targetEle, data) {
    var tree;
    layui.use('tree', function () {
        tree = layui.tree({
            elem: targetEle, //指定元素，生成的树放到哪个元素上
            check: 'checkbox', //勾选风格
            skin: 'as', //设定皮肤
            change: function (item) { //checkbox获取选中的值
                console.log(item);
                $("#detail-form").find('input[name="menuIds"]').val(item);
            },
            click: function (item) { //点击节点回调
                console.log(item);
                $("#detail-form").find('input[name="menuIds"]').val(item);
            },
            data: {//为元素添加额外数据，即在元素上添加data-xxx="yyy"，可选
                hasChild: true
            },
            nodes: data
        });
    });
}
function initDatatables() {
    var table = $('#DataTables_usermanager').DataTable({
        "paging": true,
        "searching": true,
        "info": true,
        "ordering": true,
        "language": {
            "url": "../vendor/datatables/zh-cn.json"
        }
    });
    $('#DataTables_usermanager tbody').on('click', 'tr', function () {
        var data = table.row(this).data();
    });
    var table = $('#DataTables_role').DataTable({
        "paging": true,
        "searching": true,
        "info": true,
        "ordering": true,
        "language": {
            "url": "../vendor/datatables/zh-cn.json"
        }
    });
    $('#DataTables_role tbody').on('click', 'tr', function () {
        var data = table.row(this).data();
    });
}
